/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import kr.pe.johnkim.dayve.activity.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author johnkim
 *
 */
public class DayveGroupSelectIconAdapter extends BaseAdapter {

	public static final String TAG = DayveGroupSelectIconAdapter.class.getSimpleName();
	
	private Context context;
	private ArrayList<DayveGroup> data;
	private DayveGroup item;
	LayoutInflater inf;
	
	View view;
	
	public DayveGroupSelectIconAdapter(Context context, ArrayList<DayveGroup> data) {
		this.context = context;
		this.data = data;
		inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		
		if(view == null) {
			view = inf.inflate(R.layout.dayve_group_select_icon_grid, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		item = data.get(position);
		
		// 이미지 경로의 파일명으로 drawable 디렉토리의 이미지경로를 구한다.
//		int lid = 0;
//		
//		if(!("").equals(item.getIconUrl())) {
//			lid = context.getResources().getIdentifier(item.getIconUrl(), "drawable", context.getPackageName());
//			Log.i(TAG, "image url : " + item.getIconUrl());
//		} else {
//			lid = context.getResources().getIdentifier("dayve_group_default", "drawable", context.getPackageName());
//		}
		
		int lid = context.getResources().getIdentifier(item.getIconUrl(), "drawable", context.getPackageName());
		
		wrapper.getImgViewGroupSelectGridIcon().setImageResource(lid);
		
		Log.i(TAG, "image url : " + item.getIconUrl());
		
		
		return view;
	}

	class ViewWrapper {
		View base;
		
		ImageView imgViewGroupSelectGridIcon;
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		ImageView getImgViewGroupSelectGridIcon() {
			if(imgViewGroupSelectGridIcon == null) {
				imgViewGroupSelectGridIcon = (ImageView)view.findViewById(R.id.imgViewGroupSelectGridIcon);
			}
			
			return (imgViewGroupSelectGridIcon);
		}
		
	}
}
